package com.freemyleft.left.zapp.net;

import android.content.Context;

import com.freemyleft.left.zapp.net.callback.IError;
import com.freemyleft.left.zapp.net.callback.IFailure;
import com.freemyleft.left.zapp.net.callback.IRequest;
import com.freemyleft.left.zapp.net.callback.ISuccess;
import com.freemyleft.left.zapp.ui.LoaderStyle;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by FreeMyleft on 2017/12/1.
 */

public class RestClentBuilder {
    private String mUrl = null;
    private Map<String, Object> PARAMS  = new HashMap<>();
    private IRequest mIRequest = null;
    private ISuccess mISuccess = null;
    private IFailure mIFailure = null;
    private IError mIError = null;
    private RequestBody mRequestBody = null;
    private Context mContext = null;
    private LoaderStyle mLoaderStyle = null;
    private File mFile=null;
    //下载所需
    private String mDownloadDir=null;
    private String mExtension=null;
    private String mName=null;

    RestClentBuilder() {

    }
    public final RestClentBuilder dir(String dir){
        this.mDownloadDir=dir;
        return this;

    }
    public final RestClentBuilder extension(String extension){
        this.mExtension=extension;
        return this;

    }

    public final RestClentBuilder name(String name){
        this.mName=name;
        return this;
    }
    public final RestClentBuilder url(String url) {
        this.mUrl = url;
        return this;
    }

    public final RestClentBuilder params(Map<String, Object> params) {
        PARAMS.putAll(params);
        return this;
    }

    public final RestClentBuilder params(String key, Object value) {
        PARAMS.put(key, value);
        return this;
    }
    public final RestClentBuilder file(File file) {

        this.mFile=file;
        return this;
    }
    public final RestClentBuilder file(String file) {

        this.mFile=new File(file);
        return this;
    }

    public final RestClentBuilder raw(String raw) {
        this.mRequestBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), raw);
        return this;
    }

    public final RestClentBuilder success(ISuccess iSuccess) {
        this.mISuccess = iSuccess;
        return this;
    }

    public final RestClentBuilder failure(IFailure iFailure) {
        this.mIFailure = iFailure;
        return this;
    }

    public final RestClentBuilder error(IError iError) {
        this.mIError = iError;
        return this;
    }

    public final RestClentBuilder onRequest(IRequest iRequest) {
        this.mIRequest = iRequest;
        return this;
    }

    public final RestClentBuilder loader(Context context, LoaderStyle style) {
        this.mContext = context;
        this.mLoaderStyle = style;
        return this;
    }

    /**
     * +
     * 默认loader
     *
     * @param context 上下文
     * @return
     */
    public final RestClentBuilder loader(Context context) {
        this.mContext = context;
        this.mLoaderStyle = LoaderStyle.BallSpinFadeLoaderIndicator;
        return this;
    }

    public final RestClient buid() {

        return new RestClient(mUrl, PARAMS,mDownloadDir,mExtension,mName, mISuccess, mIFailure, mIRequest, mIError, mRequestBody, mContext, mLoaderStyle,mFile);


    }
}
